<?php
namespace View;


class LoginView {
	
	//sparade variabler med cookie- och get"locations"
	private $m_userKey = "userName"; 
	private $m_passKey = "password";
	private $m_logoutKey = "logout";
	
	private $useCookieLocation = "usecookie";
	private $userCookieLocation = "usercookie";
	private $passCookieLocation = "passwordcookie";
	
	
	
	//skriver ut loginboxen
	public function DoLoginBox(){
		$xhtml =	"
					<table class='loginform'>
					<form method='POST'>
					<tr>
					<td colspan='3'><h3>Inloggning</h3></td>
					</tr>
					<tr>
					<td colspan='3'><input value='Användarnamn' class='uname login' type='text' name='".$this->m_userKey."'/></td>
					</tr>
					<tr>
					<td colspan='2'><input value='Lösenord' class='passw login' type='password' name='".$this->m_passKey."' /></td>
					<td colspan='1'>
					<input type='submit' class='login' value='Logga in!' />
					</td>
					</tr>
					<tr>
					<td><input name='".$this->useCookieLocation."' id='check' type='checkbox' /></td>
					<td><label for='check' >Kom ihåg mig</label></td>
					<tr>
					</tr>
					
					<td colspan='3'>
					<a href='?".NavigationView::NAVLOCATION."=".NavigationView::REGISTRATION."'>Registrera dig!</a>
					</td>
					</tr>
					</form>
					</table>
					";
		return $xhtml;
	}
	
	//skriver ut logout boxen
	public function DoLogoutBox(){
		$xhtml = 	"
					<form method='GET'>
					<input type='hidden' name='".$this->m_logoutKey."' value='true' />
					<input type='submit' value='Logga ut!' />
					</form>
					";
		return $xhtml;
	}
	
	//returnerar användarnamnet
	public function GetUserName(){
		if (isset( $_POST[$this->m_userKey]) == true) {
			return $_POST[$this->m_userKey];
		} else if (isset( $_COOKIE[$this->userCookieLocation])){ //om det finns en cookie sparad med användarnamn så returneras det
			return $_COOKIE[$this->userCookieLocation];
		}
		return NULL;
	}
	
	public function GetPassword(){
		if (isset( $_POST[$this->m_passKey]) == true) {
			return $_POST[$this->m_passKey];
		}else if (isset( $_COOKIE[$this->passCookieLocation])){ //om det finns en cookie sparad med lösenord så returneras det
			return $_COOKIE[$this->passCookieLocation];
		}
		return NULL;
	}
	
	public function TriedToLogin(){ 
		if( $this->GetUserName() !=NULL && $this->GetPassword() != NULL){
			return true;
		}else{
			return false;
		}
		
	}
	
	public function TriedToLogout(){
		if(isset($_GET[$this->m_logoutKey])){
			return true;
		}else{
			return false;
		}
		
	}
	
	public function UseCookie(){ //kollar om vi har tryckt i kom ihåg mig checkboxen
		return isset($_GET[$this->useCookieLocation]);
	} 
	
	public function setCookie(){ //sätter cookies med anv namn och lösen
		setcookie($this->userCookieLocation, $this->GetUserName(), time()+10000);
		setcookie($this->passCookieLocation, $this->GetPassword(), time()+10000);
		$_COOKIE[$this->userCookieLocation] = $this->GetUserName();
		$_COOKIE[$this->passCookieLocation] = $this->GetPassword();
	}
	
	public function unsetCookie(){ //tar bort cookies
		setcookie($this->userCookieLocation, "", -10);
		setcookie($this->passCookieLocation, "", -10);
		unset($_COOKIE[$this->userCookieLocation]);
		unset($_COOKIE[$this->passCookieLocation]);
	}
	
	public function cookieHasValues(){ //kollar om anv namn och lösenordcookies har något värde
		if(isset($_COOKIE[$this->userCookieLocation]) && isset($_COOKIE[$this->passCookieLocation])){
			return true;
		}
		return false;
	}
	
	public function addMessage($array){
		$html = '<ul>';
		foreach($array as $e){
			switch($e){
				case \LoginHandler::LOGIN_SUCCESS:
					$html .= "<li class='success'>Du är inloggad!</li>";
					break;
				case \LoginHandler::LOGIN_ERROR:
					$html .= "<li>Användaruppgifterna stämmer inte.</li>";
					break;
			}
		}
		$html .= "</ul>";
		return $html;
	}
	
	public function RemoveGetVar($varname) {
		$url = $_SERVER['PHP_SELF'];
	    list($urlpart, $qspart) = array_pad(explode('?', $url), 2, '');
	    parse_str($qspart, $qsvars);
	    unset($qsvars[$varname]);
	    $newqs = http_build_query($qsvars);
		header('location:'.$urlpart . '?' . $newqs);
	}
}
?>